﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tsgl
{
    public partial class loanQuery : Form
    {
        public loanQuery()
        {
            InitializeComponent();
        }

        private void loanQuery_Load(object sender, EventArgs e)
        {
            this.cboField1.Items.Add("借阅者编号");
            this.cboField1.Items.Add("姓名");
            this.cboField1.Items.Add("图书条码");
            this.cboField1.Items.Add("书目编号");
            this.cboField1.Items.Add("图书名称");
            this.cboField2.Items.Add("借阅者编号");
            this.cboField2.Items.Add("姓名");
            this.cboField2.Items.Add("图书条码");
            this.cboField2.Items.Add("书目编号");
            this.cboField2.Items.Add("图书名称");
            this.cboOperation1.Items.Add("=");
            this.cboOperation1.Items.Add(">=");
            this.cboOperation1.Items.Add("<=");
            this.cboOperation2.Items.Add("=");
            this.cboOperation2.Items.Add(">=");
            this.cboOperation2.Items.Add("<=");
            this.cboOperation0.Items.Add("And");
            this.cboOperation0.Items.Add("Or");
            this.rbExactitude.Checked = true;
            this.rbDark.Checked = false;
            this.rbOneCondition.Checked = true;
            this.rbTwoCondition.Checked = false;
            this.btnPrint.Enabled = false;
            this.dataGridView1.DataSource = queryLoanClass.getLoaninfoAll();
        }

        private void rbOneCondition_CheckedChanged(object sender, EventArgs e)
        {
            this.cboField1.SelectedIndex = 0;
            this.cboField2.SelectedIndex = -1;
            this.cboOperation0.SelectedIndex = -1;
            this.cboOperation0.Enabled = false;
            this.cboField2.Enabled = false;
            this.cboOperation2.Enabled = false;
            this.txtValue2.Enabled = false;
            this.txtValue2.Text = "";
            if (this.rbExactitude.Checked == true)
            {
                this.cboOperation1.Text = "=";
                this.cboOperation1.Enabled = true;
                this.cboOperation2.Text = "";
            }
            else if (this.rbDark.Checked == true)
            {
                this.cboOperation1.Items.Clear();
                this.cboOperation1.Items.Add("like");
                this.cboOperation1.Text = "like";
                this.cboOperation2.Text = "";
                this.cboOperation1.Enabled = false;
            }
        }

        private void rbTwoCondition_CheckedChanged(object sender, EventArgs e)
        {
            this.cboField1.SelectedIndex = 0;
            this.cboField2.SelectedIndex = 1;
            this.cboOperation0.SelectedIndex = 0;
            this.cboOperation0.Enabled = true;
            this.cboField2.Enabled = true;
            this.cboOperation2.Enabled = true;
            this.txtValue2.Text = "";
            this.txtValue2.Enabled = true;
            if (this.rbExactitude.Checked == true)
            {
                this.cboOperation1.Text = "=";
                this.cboOperation2.Text = "=";
                this.cboOperation1.Enabled = true;
            }
            else if (this.rbDark.Checked == true)
            {
                this.cboOperation1.Items.Add("like");
                this.cboOperation2.Items.Add("like");
                this.cboOperation1.Text = "like";
                this.cboOperation2.Text = "like";
                this.cboOperation1.Enabled = false;
                this.cboOperation2.Enabled = false;
            }
        }

        private void rbExactitude_CheckedChanged(object sender, EventArgs e)
        {
            if(this.rbOneCondition.Checked==true)
            {
                this.cboOperation1.Items.Clear();
                this.cboOperation1.Items.Add("=");
                this.cboOperation1.Items.Add(">=");
                this.cboOperation1.Items.Add("<=");
                this.cboOperation1.Text="=";
                this.cboOperation2.Text="";
                this.cboOperation0.SelectedIndex=-1;
                this.cboOperation1.Enabled = true;
            }
            else if(this.rbTwoCondition.Checked==true)
            {
                this.cboOperation1.Items.Clear();
                this.cboOperation1.Enabled = true;
                this.cboOperation1.Items.Add("=");
                this.cboOperation1.Items.Add(">=");
                this.cboOperation1.Items.Add("<=");
                this.cboOperation1.Text = "=";
                this.cboOperation2.Enabled = true;
                this.cboOperation2.Items.Clear();
                this.cboOperation2.Items.Add("=");
                this.cboOperation2.Items.Add(">=");
                this.cboOperation2.Items.Add("<=");
                this.cboOperation2.Text = "=";
                this.cboOperation0.SelectedIndex = 0;
            }
        }

        private void rbDark_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbOneCondition.Checked == true)
            {
                this.cboOperation1.Items.Add("like");
                this.cboOperation1.Text = "like";
                this.cboOperation1.Enabled = false;
                this.cboOperation2.Text = "";
                this.cboOperation0.SelectedIndex = -1;
            }
            else if (this.rbTwoCondition.Checked == true)
            {
                this.cboOperation1.Items.Add("like");
                this.cboOperation2.Items.Add("like");
                this.cboOperation1.Text = "like";
                this.cboOperation2.Text = "like";
                this.cboOperation1.Enabled = false;
                this.cboOperation2.Enabled = false;
                this.cboOperation0.SelectedIndex = 0;
            }
        }

       
        private void btnFind_Click(object sender, EventArgs e)
        {
            //string strCondition = "select 借阅者编号,姓名,图书条码,书目编号,图书名称,价格,借出日期,应还日期,续借次数,图书借阅员 from loanView where ";
            if (this.rbOneCondition.Checked == true || this.rbExactitude.Checked == true)
            {
                string strCondition = "select 借阅者编号,姓名,图书条码,书目编号,图书名称,价格,借出日期,应还日期,续借次数,图书借阅员 from loanView where " + this.cboField1.Text + " " + this.cboOperation1.Text + " '" + this.txtValue1.Text.Trim() + "'";
                MessageBox.Show(strCondition);
                this.dataGridView1.DataSource = bookDbClass.ExecuteDataTable(strCondition);
                this.btnPrint.Enabled = true;
            }
            if (this.rbOneCondition.Checked == true || this.rbDark.Checked == true)
            {
                string strCondition = "select 借阅者编号,姓名,图书条码,书目编号,图书名称,价格,借出日期,应还日期,续借次数,图书借阅员 from loanView where ";
                strCondition = strCondition + this.cboField1.Text + " " + this.cboOperation1.Text + " " + "'%" + this.txtValue1.Text.Trim() + "%'";
                //MessageBox.Show(strCondition);
                this.dataGridView1.DataSource = bookDbClass.ExecuteDataTable(strCondition);
            }
            if (this.rbTwoCondition.Checked == true || this.rbExactitude.Checked == true)
            {
                //string strCondition = this.cboField1.Text + this.cboOperation1.Text + this.txtValue1.Text.Trim() + " " + this.cboOperation0.Text + " " + this.cboField2.Text + this.cboOperation2.Text + this.txtValue2.Text.Trim();
                string strCondition = "select 借阅者编号,姓名,图书条码,书目编号,图书名称,价格,借出日期,应还日期,续借次数,图书借阅员 from loanView where ";
                strCondition = strCondition + this.cboField1.Text + " " + this.cboOperation1.Text + " " + this.txtValue1.Text.Trim() + " " + this.cboOperation0.Text + " " + this.cboField2.Text + " " + this.cboOperation2.Text + " " + this.txtValue2.Text.Trim();
               // MessageBox.Show(strCondition);
                this.dataGridView1.DataSource = bookDbClass.ExecuteDataTable(strCondition);
            }
            if (this.rbTwoCondition.Checked == true || this.rbDark.Checked == true)
            {
                //string strCondition = this.cboField1.Text + this.cboOperation1.Text + "%" + this.txtValue1.Text.Trim() + "%" + this.cboOperation0.Text + " " + this.cboField2.Text + " " + this.cboOperation2.Text + "%" + this.txtValue2.Text.Trim() + "%";
                string strCondition = "select 借阅者编号,姓名,图书条码,书目编号,图书名称,价格,借出日期,应还日期,续借次数,图书借阅员 from loanView where ";
                strCondition = strCondition + this.cboField1.Text +" "+ this.cboOperation1.Text +" "+ "'%" + this.txtValue1.Text.Trim() + "%'" +" "+ this.cboOperation0.Text + " " + this.cboField2.Text + " " + this.cboOperation2.Text +" "+ "'%" + this.txtValue2.Text.Trim() + "%'";
                //MessageBox.Show(strCondition);
                this.dataGridView1.DataSource = bookDbClass.ExecuteDataTable(strCondition);
            }
        }

        private void btnFindAll_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = queryLoanClass.getLoaninfoAll();
            this.btnPrint.Enabled = true;
        }

        private void btnOverdueInfo_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = queryLoanClass.getOverdueInfo();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtValue1_KeyDown(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.btnFind;
        }

        private void txtValue2_KeyDown(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.btnFind;
        }
    }
}